package com.bishe.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bishe.entity.Order;
import com.bishe.entity.OrderQuery;
import com.bishe.entity.response.R;
import com.bishe.service.OrderService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * (Order)表控制层
 *
 * @author makejava
 * @since 2024-03-07 14:47:04
 */
@Api(tags = "用户订单控制器")
@RestController
@RequestMapping("order")
public class OrderController {
    /**
     * 服务对象
     */
    @Resource
    private OrderService orderService;

    @GetMapping("tj")
    public R tj() {
        List<Order> orderList = orderService.list();
        if (CollUtil.isEmpty(orderList)) {
            return R.ok();
        }
        Map<Integer, List<Order>> collect =
                orderList.stream().collect(Collectors.groupingBy(Order::getStatus));

        Map<Object, Object> map = new HashMap<>();
        for (Map.Entry<Integer, List<Order>> integerListEntry : collect.entrySet()) {
            map.put(integerListEntry.getKey(), integerListEntry.getValue().size());
        }

        List<Integer> list = Arrays.asList(0, 1, 2, 3);

        Set<Object> objects = map.keySet();
        for (Integer i : list) {
            if (!objects.contains(i)) {
                map.put(i, 0);
            }
        }
        return R.ok(map);
    }


    @GetMapping("updateStatus/{id}/{status}")
    public R updateStatus(@PathVariable String id, @PathVariable Integer status) {
        Order byId = orderService.getById(id);
        byId.setStatus(status);
        return R.ok(orderService.updateById(byId));
    }


    @PostMapping("myOrders")
    public R myOrders(@RequestBody OrderQuery query) {
        return R.ok(orderService.myOrders(query));
    }


    /**
     * 分页查询所有数据
     *
     * @param page  分页对象
     * @param order 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<Order> page, Order order) {
        return R.ok(this.orderService.getPage(page, order));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return R.ok(this.orderService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param order 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody Order order) {
        order.setOrderNo("FS" + System.currentTimeMillis());
        return R.ok(this.orderService.save(order));
    }

    /**
     * 修改数据
     *
     * @param order 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Order order) {
        return R.ok(this.orderService.updateById(order));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.ok(this.orderService.removeByIds(idList));
    }
}

